package vek.GUI.MapView;

import java.util.Observable;
import javax.swing.*;
import java.awt.*;
import java.awt.Dimension;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.Observer;
import vek.ObjectDatabase.DatabaseConnector;
import vek.Objects.POI;
import vek.TerminalSettings;
import vek.controllers.Map.MarkingsViewController;

/**
 * Handles the graphical representation of the icons showing the location of the POIs on the map i.e. it marks them on top of the background image (see the class LayerHandler).
 * The location of the icons are determined by a database, which are interacted with through the DatabaseConnector-class.
 * @author Christian, Michal og Jakob (Hold 1, gruppe 2)
 */

public class MarkingsView extends JPanel implements Observer{
    
    private MarkingsViewController controller;
    private DatabaseConnector databaseConnector;
    private ArrayList<MarkingButton> markings;
    
    /**
     * Creates the MarkingsView. This is done by first making the MarkingsView transparent (empty) and making sure that it is using a pixle-based grid layout.
     * Then the MarkingButtons are created and subsequently  added at the rights spots on the MarkingsView.
     * @param size The preffered size of this component
     * @param mvc the MarkingsViewController which controls when the MarkingButtons are clicked
     * @param dbc The database connector which gives indirect access to the databases
     */
    

    public MarkingsView(Dimension size, MarkingsViewController mvc, DatabaseConnector dbc)
    {
        controller = mvc;
        databaseConnector = dbc;
        
        setOpaque(false);
        setPreferredSize(size);
             
        createLayout();
        createAllMarkingButtons();
        addAllMarkingButtons();
        
        
    }
    
    public void paintComponent(Graphics arg0)
    {
        Graphics g = arg0;
        g = (Graphics2D) g;
        Font f = new Font(Font.SERIF, Font.BOLD, 20);
        g.setFont(f);
        g.setColor(Color.RED);
        g.drawString("Du er her", TerminalSettings.getTerminalXCoord() - 45, TerminalSettings.getTerminalYCoord() - 30);
        g.fillOval(TerminalSettings.getTerminalXCoord() - 20, TerminalSettings.getTerminalYCoord() - 20, 20, 20);
    }
            
    

    /**
     * Creates the markingButtons which are to be shown in this component.
     */
    
    private void createAllMarkingButtons()
    {
        markings = new ArrayList<MarkingButton>();
        ArrayList<POI> allPOIs = databaseConnector.getAll();
        for(int i = 0; i < allPOIs.size(); i++)
        {
            POI poi = allPOIs.get(i);
            markings.add(new MarkingButton(poi.getLocation(), controller, poi.getName(), poi));
        }
    }

    /**
     * adds the MarkingButtons to this component so that they are shown
     */
    
    private void addAllMarkingButtons()
    {
        for(int i = 0; i < markings.size(); i++)
        {
            add(markings.get(i));
        }
    }
    
    /**
     * creates the layout for this component, uses the null layout which means that positioning is absolute and pixel based.
     */
    
    private void createLayout()
    {
        setLayout(null);   
    }
    
    /**
     * removes all the markingbuttons from this component
     */
    
    private void removeAllMarkings()
    {
        removeAll();
    }
    
    
    /**
     * Updates this component by getting a new list of the POIs from the DatabaseConnector.
     * This method gets a new list of the new POIs when the databases are updated.
     * 
     * @param o the observable object, in this case the DatabaseConnector
     * @param arg not used
     */

    public void update(Observable o, Object arg) {
        
        removeAllMarkings();
        createAllMarkingButtons();
        addAllMarkingButtons();
        repaint();
        
    }

}
